<template>
  <a-modal
    title="客户(商圈)管理"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务商"
        >
          <a-tree-select :disabled="isDisabled"
                         :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                         placeholder='请选择'
                         allowClear
                         treeDefaultExpandAll
                         :treeData="treeData"
                         v-decorator="[ 'parentCode', {rules: [{ required: true, message: '请选择。。。' }]} ]"
          >
              <span style="color:#08c" slot="title" slot-scope="{key}">
                {{value}}
              </span>>
          </a-tree-select>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户(商圈)名称">
          <a-input placeholder="客户(商圈)名称" v-decorator="[ 'name', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地址">
          <a-input placeholder="地址" v-decorator="[ 'address', {rules: [] }]"/>
        </a-form-item>

        <!--<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户编号" v-if="isDisabled">
          <a-input :disabled="isDisabled" placeholder="客户编号" v-decorator="[ 'customerNo', {rules: [] }]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="api密匙" v-if="isDisabled">
          <a-input-search placeholder="api密匙"
                          v-decorator="[ 'apiKey', {rules: [{ required: false, message: '请输入。。。' }]}]"
                          :disabled="isDisabled" @search="onSearch">
            <a-button slot="enterButton" type="primary">刷新</a-button>
          </a-input-search>
        </a-form-item>-->

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="小程序appid">
          <a-input placeholder="小程序appid" v-decorator="[ 'miniAppid', {rules: [] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="小程序secret">
          <a-input placeholder="小程序secret" v-decorator="[ 'miniSecret', {rules: [] }]"/>
        </a-form-item>

        <!--<a-divider>九宫格配置</a-divider>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="九宫格配置"
        >
          <a-select mode="multiple"
                    v-decorator="[ 'gridList', {rules: [{ required: true, message: '请选择' }]} ]">
            <a-select-option v-for="item in grids" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>-->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveOrUpdate, refreshApi, selectGrids } from '@/api/shop/customer'
  import { selectAgentTree } from '@/api/system/company'

  export default {
    data () {
      return {
        mdl:{},
        id: null,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        isDisabled: false,
        treeData: [],
        grids: [],

        form: this.$form.createForm(this)
      }
    },
    mounted () {
      selectAgentTree().then(res => {
        if (res.code === 200) {
          this.treeData = res.result
        }
      })
      // selectGrids().then(res => {
      //   if (res.code === 200) {
      //     this.grids = res.result
      //   }
      // })
    },
    methods: {
      add () {
        this.visible = true
      },
      edit (record) {
        this.id = record.id
        this.visible = true
        this.isDisabled = true
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          // this.mdl = Object.assign({}, record);
          const formData = pick(record, ['parentCode', 'name', 'address', 'customerNo', 'apiKey', 'miniAppid', 'miniSecret'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            values.type = 2 //代理商
            saveOrUpdate(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.visible = false
        this.isDisabled = false
        this.form.resetFields()
        this.id = null
      },
      onSearch () {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `刷新之后会导致以前的接口凭证无法使用`,
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          onOk () {
            // 在这里调用删除接口
            refreshApi(_this.id).then(record => {
              if (record.code == 200) {
                _this.handleCancel()
                _this.$emit('ok')
              }
            })
          },
          onCancel () {
            console.log('Cancel')
          }
        })
      }
    }
  }
</script>
